var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    linkmanGuid:'',
    phonenumber:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  toAddtxl: function (event) {
    wx.navigateTo({
      url: "../addCategory/addCategory"
    })
  },
  toAddlinkman: function (event) {
    wx.navigateTo({
      url: "../addLinkman/addLinkman"
    })
  },
  bindphone: function (event) {
    let that = this
    var phoneNumber = wx.getStorageSync('phoneNumber')
    this.setData({
      phonenumber: phoneNumber,
    })
    console.log(phoneNumber)
    if (!phoneNumber) {
      wx.navigateTo({
        url: "../authorize_phone/authorize_phone"
      })
    } else {
        //通过电话号码获取联系人信息
      getLinkmanByPhoneNum(that);
    }
  },
})
//获取数据
var getLinkmanByPhoneNum = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/getLinkmanByPhoneNum', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        linkmanTelephone: self.data.phonenumber
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        self.setData({
          linkmanGuid: res.data.UserArea.linkmanGuid,
        })
        wx.showModal({
          title: '提示',
          content: '该手机号：' + self.data.phonenumber + '在通讯录中属于' + res.data.UserArea.linkmanName+'，是否与之绑定？',
          confirmText: "确定",
          cancelText: "取消",
          success: function (res) {
            console.log(res);
            if (res.confirm) {
              console.log('用户点击主操作')
              BandTelephone(self);
            } else {
              console.log('用户点击辅助操作')
            }
          }
        });
      }else{
        wx.showModal({
          content: '该号码未在通讯录中使用，无法进行绑定。',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }
    }
  })
}
//绑定账号
var BandTelephone = function (self) {
  let userInfo = wx.getStorageSync('userInfo')
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/updateLinkman', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        linkmanGuid: self.data.linkmanGuid,
        userGuid: app.globalData.OPEN_ID,
        linkmanHeadUrl: userInfo.avatarUrl,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showModal({
          content: '绑定成功,您可以修改该用户的信息了',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }else{
        wx.showModal({
          content: '绑定失败，原因不知',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }
    }
  })
}